package cn.fintecher.gis.web;


import cn.fintecher.gis.entity.LayerPublish;
import cn.fintecher.gis.entity.LayerStatistical;
import cn.fintecher.gis.enums.Rapporttype;
import cn.fintecher.gis.model.request.LayerPublishRequest;
import cn.fintecher.gis.model.response.LayerStatisticalResponse;
import cn.fintecher.gis.service.LayerStatisticalService;
import cn.fintecher.gis.utils.BeanUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.stream.Collectors;

@RestController
@RequestMapping("/api/layerStatistical")
@Api(value = "图层统计管理", description = "图层统计管理")
public class LayerStatisticalController {

    @Autowired
    private LayerStatisticalService layerStatisticalService;

    @PostMapping("/addLayerPublish")
    @ApiOperation(value = "根据条件发布对应的数据统计", notes = "根据条件发布对应的数据统计")
    public ResponseEntity<Void> addLayerPublish(@RequestBody LayerPublishRequest layerPublish) throws Exception {
        layerStatisticalService.addPublish(layerPublish);
        return ResponseEntity.ok().build();
    }

    @GetMapping("/getLayerPublish")
    @ApiOperation(value = "对发布的统计图层进行查询", notes = "对发布的统计图层进行查询")
    public ResponseEntity<List<LayerPublish>> getLayerPublish() {
        List<LayerPublish> list = layerStatisticalService.getLayerPublishList();
        return ResponseEntity.ok().body(list);
    }

    @DeleteMapping("/deleteLayerPublish/{id}")
    @ApiOperation(value = "删除操作", notes = "删除操作")
    public ResponseEntity<Void> deleteLayerPublish(@PathVariable String id) {
        layerStatisticalService.deleteLayerPublishById(id);
        return ResponseEntity.ok().build();
    }

    @GetMapping("/getLayerStatisticalReport")
    @ApiOperation(value = "查询除了土地利用现状一级分类统计的其他三种类型", notes = "查询除了土地利用现状一级分类统计的其他三种类型")
    public ResponseEntity<List<LayerStatisticalResponse>> getLayerStatistical(@RequestParam("reportType") Rapporttype reportType) {
        List<LayerStatistical> list = layerStatisticalService.getLayerStatisticalReport(reportType);
        List<LayerStatisticalResponse> responseList = list.stream().map(layerStatistical -> {
            LayerStatisticalResponse response = new LayerStatisticalResponse();
            BeanUtils.copyPropertiesIgnoreNull(layerStatistical, response);
            response.setReportType(reportType);
            return response;
        }).collect(Collectors.toList());
        return ResponseEntity.ok().body(responseList);
    }

    @GetMapping("/getClassificationOne")
    @ApiOperation(value = "土地利用现状一级分类统计", notes = "土地利用现状一级分类统计")
    public ResponseEntity<List<LayerStatisticalResponse>> getClassificationOne(@RequestParam("year") String year) {
        List<LayerStatistical> list = layerStatisticalService.getClassificationOne(year);
        List<LayerStatisticalResponse> responseList = list.stream().map(layerStatistical -> {
            LayerStatisticalResponse response = new LayerStatisticalResponse();
            BeanUtils.copyPropertiesIgnoreNull(layerStatistical, response);
            response.setReportType(Rapporttype.LAND_CLASSIFICATION);//土地利用现状一级分类统计
            return response;
        }).collect(Collectors.toList());
        return ResponseEntity.ok().body(responseList);
    }

    @GetMapping("/getSecondClassification")
    @ApiOperation(value = "土地利用现状二级分类统计", notes = "土地利用现状二级分类统计")
    public ResponseEntity<List<LayerStatisticalResponse>> getSecondClassification(@RequestParam("publishId") String publishId,
                                                                                  @RequestParam("code") String code) {
        List<LayerStatistical> list = layerStatisticalService.getSecondClassification(publishId, code);
        List<LayerStatisticalResponse> responseList = list.stream().map(layerStatistical -> {
            LayerStatisticalResponse response = new LayerStatisticalResponse();
            BeanUtils.copyPropertiesIgnoreNull(layerStatistical, response);
            return response;
        }).collect(Collectors.toList());
        return ResponseEntity.ok().body(responseList);
    }
}
